<?php

namespace App\Traits;

use App\Models\Tag;

trait HasTags
{
    public static function bootHasTags(): void
    {
        static::saved(function ($model) {
            $model->tags()->sync(\request()->input('tag_ids'));
        });

        static::deleted(function ($model) {
            $model->tags()->detach();
        });
    }

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'taggable');
    }
}
